<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.4: http://docutils.sourceforge.net/" />
<title>Low-Level I/O</title>
<meta name="author" content="Phil Buonadonna, Jonathan Hui" />
<style type="text/css">

/*
:Author: David Goodger
:Contact: goodger@users.sourceforge.net
:date: $Date: 2009-02-03 23:07:32 $
:version: $Revision: 1.10 $
:copyright: This stylesheet has been placed in the public domain.

Default cascading style sheet for the HTML output of Docutils.
*/
body {
  font-family: Times;
  font-size: 16px;
}

.first {
  margin-top: 0 ! important }

.last {
  margin-bottom: 0 ! important }

.hidden {
  display: none }

a.toc-backref {
  text-decoration: none ;
  color: black }

blockquote.epigraph {
  margin: 2em 5em ; }

dd {
  margin-bottom: 0.5em }

div.abstract {
  margin: 2em 5em }

div.abstract p.topic-title {
  font-weight: bold ;
  text-align: center }

div.attention, div.caution, div.danger, div.error, div.hint,
div.important, div.note, div.tip, div.warning, div.admonition {
  margin: 2em ;
  border: medium outset ;
  padding: 1em }

div.attention p.admonition-title, div.caution p.admonition-title,
div.danger p.admonition-title, div.error p.admonition-title,
div.warning p.admonition-title {
  color: red ;
  font-weight: bold ;
  font-family: sans-serif }

div.hint p.admonition-title, div.important p.admonition-title,
div.note p.admonition-title, div.tip p.admonition-title,
div.admonition p.admonition-title {
  font-weight: bold ;
  font-family: sans-serif }

div.dedication {
  margin: 2em 5em ;
  text-align: center ;
  font-style: italic }

div.dedication p.topic-title {
  font-weight: bold ;
  font-style: normal }

div.figure {
  margin-left: 2em }

div.footer, div.header {
  font-size: smaller }

div.line-block {
  display: block ;
  margin-top: 1em ;
  margin-bottom: 1em }

div.line-block div.line-block {
  margin-top: 0 ;
  margin-bottom: 0 ;
  margin-left: 1.5em }

div.sidebar {
  margin-left: 1em ;
  border: medium outset ;
  padding: 0em 1em ;
  background-color: #ffffee ;
  width: 40% ;
  float: right ;
  clear: right }

div.sidebar p.rubric {
  font-family: sans-serif ;
  font-size: medium }

div.system-messages {
  margin: 5em }

div.system-messages h1 {
  color: red }

div.system-message {
  border: medium outset ;
  padding: 1em }

div.system-message p.system-message-title {
  color: red ;
  font-weight: bold }

div.topic {
  margin: 2em }

h1 {
  font-family: Arial, sans-serif;
  font-size: 20px;
}

h1.title {
 text-align: center;
 font-size: 32px;
}

h2 {
 font-size: 16px;
 font-family: Arial, sans-serif;
}

h2.subtitle {
  text-align: center }

h3 {
 font-size: 12px;
 font-family: Arial, sans-serif;
}

hr {
  width: 75% }

ol.simple, ul.simple {
  margin-bottom: 1em }

ol.arabic {
  list-style: decimal }

ol.loweralpha {
  list-style: lower-alpha }

ol.upperalpha {
  list-style: upper-alpha }

ol.lowerroman {
  list-style: lower-roman }

ol.upperroman {
  list-style: upper-roman }

p.attribution {
  text-align: right ;
  margin-left: 50% }

p.caption {
  font-style: italic }

p.credits {
  font-style: italic ;
  font-size: smaller }

p.label {
  white-space: nowrap }

p.rubric {
  font-weight: bold ;
  font-size: larger ;
  color: maroon ;
  text-align: center }

p.sidebar-title {
  font-family: sans-serif ;
  font-weight: bold ;
  font-size: larger }

p.sidebar-subtitle {
  font-family: sans-serif ;
  font-weight: bold }

p.topic-title {
  font-weight: bold }

pre.address {
  margin-bottom: 0 ;
  margin-top: 0 ;
  font-family: serif ;
  font-size: 100% }

pre.line-block {
  font-family: serif ;
  font-size: 100% }

pre.literal-block, pre.doctest-block {
  margin-left: 2em ;
  margin-right: 2em ;
  background-color: #eeeeee;
  border-color: #000000;
  border-width: thin; 
  font-size: 14px
}

span.classifier {
  font-family: sans-serif ;
  font-style: oblique }

span.classifier-delimiter {
  font-family: sans-serif ;
  font-weight: bold }

span.interpreted {
  font-family: sans-serif }

span.option {
  white-space: nowrap }

span.option-argument {
  font-style: italic }

span.pre {
  white-space: pre }

span.problematic {
  color: red }

table {
  margin-top: 0.5em ;
  margin-bottom: 0.5em }

table.citation {
  border-left: solid thin gray ;
  padding-left: 0.5ex }

table.docinfo {
  margin: 2em 4em;
}

table.footnote {
  border-left: solid thin black ;
  padding-left: 0.5ex }

td, th {
  padding-left: 0.5em ;
  padding-right: 0.5em ;
  vertical-align: top }

th.docinfo-name, th.field-name {
  font-weight: bold ;
  text-align: left ;
  white-space: nowrap;
  }

h1 tt, h2 tt, h3 tt, h4 tt, h5 tt, h6 tt {
  font-size: 100% }

tt {}

ul.auto-toc {
  list-style-type: none }

</style>
</head>
<body>
<div class="document" id="low-level-i-o">
<h1 class="title">Low-Level I/O</h1>
<table class="docinfo" frame="void" rules="none">
<col class="docinfo-name" />
<col class="docinfo-content" />
<tbody valign="top">
<tr class="field"><th class="docinfo-name">TEP:</th><td class="field-body">117</td>
</tr>
<tr class="field"><th class="docinfo-name">Group:</th><td class="field-body">Core Working Group</td>
</tr>
<tr class="field"><th class="docinfo-name">Type:</th><td class="field-body">Documentary</td>
</tr>
<tr><th class="docinfo-name">Status:</th>
<td>Final</td></tr>
<tr class="field"><th class="docinfo-name">TinyOS-Version:</th><td class="field-body">2.x</td>
</tr>
<tr><th class="docinfo-name">Author:</th>
<td>Phil Buonadonna, Jonathan Hui</td></tr>
</tbody>
</table>
<div class="note">
<p class="first admonition-title">Note</p>
<p class="last">This memo documents a part of TinyOS for the TinyOS Community, and
requests discussion and suggestions for improvements.  Distribution
of this memo is unlimited. This memo is in full compliance with
TEP 1.</p>
</div>
<div class="section">
<h1><a id="abstract" name="abstract">Abstract</a></h1>
<p>The memo documents the TinyOS 2.x interfaces used for controlling
digital IO functionality and digital interfaces.</p>
</div>
<div class="section">
<h1><a id="introduction" name="introduction">1. Introduction</a></h1>
<p>The canonical TinyOS device is likely to have a variety of digital
interfaces. These interfaces may be divided into two broad
categories. The first are general purpose digital I/O lines (pins) for
individual digital signals at physical pins on a chip or platform. The
second are digital I/O interfaces that have predefined communication
protocol formats. The three buses covered in this document are the
Serial Peripheral Interface (SPI), the Inter-Integrated Circuit (I2C)
or Two-Wire interface, and the Universal Asynchronous
Receiver/Transmitter (UART) interface. While there are likely other
bus formats, we presume SPI, I2C, and UART to have the largest
coverage.</p>
<p>This memo documents the interfaces used for pins and the three buses.</p>
</div>
<div class="section">
<h1><a id="pins" name="pins">2. Pins</a></h1>
<p>General Purpose I/O (GPIO) pins are single, versatile digital I/O
signals individually controllable on a particular chip or
platform. Each GPIO can be placed into either an input mode or an
output mode. On some platforms a third 'tri-state' mode may exist, but
this functionality is platform specific and will not be covered in
this document.</p>
<p>On many platforms, a physical pin may function as either a digital
GPIO or another special function I/O such. Examples include ADC I/O or
a bus I/O. Interfaces to configure the specific function of a pin are
platform specific.</p>
<p>The objective of the interfaces described here is not to attempt to
cover all possibilities of GPIO functionality and features, but to
distill down to a basis that may be expected on most platforms.</p>
<dl class="docutils">
<dt>In input mode, we assume the following capabilities:</dt>
<dd><ul class="first last simple">
<li>The ability to arbitrarily sample the pin</li>
<li>The ability to generate an interrupt/event from either a rising edge or falling edge digital signal.</li>
</ul>
</dd>
<dt>In output mode, we assume the following capabilities:</dt>
<dd><ul class="first last simple">
<li>An I/O may be individually cleared (low) or set (hi)</li>
</ul>
</dd>
</dl>
<p>Platform that provide GPIO capabilities MUST provide the following HIL
interfaces:</p>
<blockquote>
<ul class="simple">
<li>GeneralIO</li>
<li>GpioInterrupt</li>
</ul>
</blockquote>
<p>Platforms MAY provide the following capture interface.</p>
<blockquote>
<ul class="simple">
<li>GpioCapture</li>
</ul>
</blockquote>
<div class="section">
<h2><a id="generalio" name="generalio">2.1 GeneralIO</a></h2>
<p>The GeneralIO HIL interface is the fundamental mechanism for controlling a
GPIO pin. The interface provides a mechanism for setting the pin mode
and reading/setting the pin value. The toggle function switches the
output state to the opposite of what it currently is.</p>
<p>Platforms with GPIO functionality MUST provide this interface. It
SHOULD be provided in a component named GeneralIOC, but MAY be
provided in other components as needed.</p>
<pre class="literal-block">
interface GeneralIO
{
  async command void set();
  async command void clr();
  async command void toggle();
  async command bool get();
  async command void makeInput();
  async command bool isInput();
  async command void makeOutput();
  async command bool isOutput();
}
</pre>
</div>
<div class="section">
<h2><a id="gpiointerrupt" name="gpiointerrupt">2.2 GpioInterrupt</a></h2>
<p>The GPIO Interrupt HIL interface provides baseline event control for a
GPIO pin. It provides a mechanism to detect a rising edge OR a falling
edge. Note that calls to enableRisingEdge and enableFallingEdge are
NOT cumulative and only one edge may be detected at a time. There may
be other edge events supported by the platform which MAY be exported
through a platform specific HAL interface.</p>
<pre class="literal-block">
interface GpioInterrupt {

  async command error_t enableRisingEdge();
  async command bool isRisingEdgeEnabled();
  async command error_t enableFallingEdge();
  async command bool isFallingEdgeEnabled();
  async command error_t disable();
  async event void fired();

}
</pre>
</div>
<div class="section">
<h2><a id="gpiocapture" name="gpiocapture">2.3 GpioCapture</a></h2>
<p>The GpioCapture interface provides a means of associating a timestamp
with a GPIO event. Platforms MAY provide this interface.</p>
<p>Some platforms may have hardware support for such a feature. Other
platforms may emulate this capability using the SoftCaptureC
component. The interface makes not declaration of the precision or
accuracy of the timestamp with respect to the associated GPIO
event.</p>
<pre class="literal-block">
interface GpioCapture {

  async command error_t captureRisingEdge();
  async command bool isRisingEdgeEnabled();
  async command error_t captureFallingEdge();
  async command bool isFallingEdgeEnabled();
  async event void captured(uint16_t time);
  async command void disable();

}
</pre>
</div>
</div>
<div class="section">
<h1><a id="buses" name="buses">3. Buses</a></h1>
<p>Bus operations may be divided into two categories: data and
control. The control operations of a particular bus controller are
platform specific and not covered here. Instead, we focus on the data
interfaces at the HIL level that are expected to be provided.</p>
<div class="section">
<h2><a id="serial-peripheral-interface" name="serial-peripheral-interface">3.1 Serial Peripheral Interface</a></h2>
<p>The Serial Peripheral Interface (SPI) is part of a larger class of
Synchronous Serial Protocols.  The term SPI typically refers to the
Motorola SPI protocols. Other protocols include the National
Semiconductor Microwire, the TI Synchronous Serial Protocol and the
Programmable Serial Protocol. The dataside interfaces here were
developed for the Motorola SPI format, but may work for others.</p>
<p>Platforms supporting SPI MUST provide these interfaces.</p>
<p>Of note, the interfaces DO NOT define the behavior of any chip select
or framing signals. These SHOULD determined by platform specific HAL
interfaces and implementations.</p>
<p>The interface is split into a synchronous byte level and an
asynchronous packet level interface. The byte level interface is
intended for short transactions (3-4 bytes) on the SPI bus.</p>
<pre class="literal-block">
interface SpiByte {
  async command uint8_t write( uint8_t tx );
}
</pre>
<p>The packet level interface is for larger bus transactions. The
pointer/length interface permits use of hardware assist such as
DMA.</p>
<pre class="literal-block">
interface SpiPacket {
  async command error_t send( uint8_t* txBuf, uint8_t* rxBuf, uint16_t len );
  async event void sendDone( uint8_t* txBuf, uint8_t* rxBuf, uint16_t len,
                             error_t error );
}
</pre>
</div>
<div class="section">
<h2><a id="i2c" name="i2c">3.2 I2C</a></h2>
<p>The Inter-Integrated Circuit (I2C) interface is another type of
digital bus that is often used for chip-to-chip communication. It is
also known as a two-wire interface.</p>
<p>The I2CPacket interface provides for asynchronous Master mode
communication on an I2C with application framed packets. Individual
I2C START-STOP events are controllable which allows the using
component to do multiple calls within a single I2C transaction and
permits multiple START sequences</p>
<p>Platforms providing I2C capability MUST provide this interface.</p>
<pre class="literal-block">
interface I2CPacket&lt;addr_size&gt; {
  async command error_t read(i2c_flags_t flags, uint16_t addr, uint8_t length, u int8_t* data);
  async event void readDone(error_t error, uint16_t addr, uint8_t length, uint8_t* data);
  async command error_t write(i2c_flags_t flags, uint16_t addr, uint8_t length, uint8_t* data);
  async event void writeDone(error_t error, uint16_t addr, uint8_t length, uint8_t* data)
}
</pre>
<p>The interface is typed according to the addressing space the
underlying implementation supports.  Valid type values are below.</p>
<pre class="literal-block">
TI2CExtdAddr - Interfaces uses the extended (10-bit) addressing mode.
TI2CBasicAddr - Interfaces uses the basic (7-bit) addressing mode.
</pre>
<p>The i2c_flags_t values are defined below. The flags define the
behavior of the operation for the call being made. These values may be
ORed together.</p>
<pre class="literal-block">
I2C_START    - Transmit an I2C STOP at the beginning of the operation.
I2C_STOP     - Transmit an I2C STOP at the end of the operation. Cannot be used
               with the I2C_ACK_END flag.
I2C_ACK_END  - ACK the last byte sent from the buffer. This flags is only valid
               a write operation. Cannot be used with the I2C_STOP flag.
</pre>
</div>
<div class="section">
<h2><a id="uart" name="uart">3.3 UART</a></h2>
<p>The Universal Asynchronous Receiver/Transmitter (UART) interface is a
type of serial interconnect. The interface is &quot;asynchronous&quot; since it
recovers timing from the data stream itself, rather than a separate
control stream. The interface is split into an asynchronous multi-byte
level interface and a synchronous single-byte level interface.</p>
<p>The multi-byte level interface, UartStream, provides a split-phase
interface for sending and receiving one or more bytes at a time. When
receiving bytes, a byte-level interrupt can be enabled or an interrupt
can be generated after receiving one or more bytes. The latter is
intended to support use cases where the number of bytes to receive is
already known. If the byte-level receive interrupt is enabled, the
receive command MUST return FAIL. If a multi-byte receive interrupt is
enabled, the enableReceiveInterrupt command MUST return FAIL.</p>
<pre class="literal-block">
interface UartStream {
  async command error_t send( uint8_t* buf, uint16_t len );
  async event void sendDone( uint8_t* buf, uint16_t len, error_t error );

  async command error_t enableReceiveInterrupt();
  async command error_t disableReceiveInterrupt();
  async event void receivedByte( uint8_t byte );

  async command error_t receive( uint8_t* buf, uint8_t len );
  async event void receiveDone( uint8_t* buf, uint16_t len, error_t error );
}
</pre>
<p>The single-byte level interface, UartByte, provides a synchronous
interface for sending and receiving a single byte. This interface is
intended to support use cases with short transactions. Because UART is
asynchronous, the receive command takes a timeout which represents
units in byte-times, after which the command returns with an
error. Note that use of this interface is discouraged if the UART baud
rate is low.</p>
<pre class="literal-block">
interface UartByte {
  async command error_t send( uint8_t byte );
  async command error_t receive( uint8_t* byte, uint8_t timeout );
}
</pre>
</div>
</div>
<div class="section">
<h1><a id="implementation" name="implementation">4. Implementation</a></h1>
<p>Example implementations of the pin interfaces can be found in tos/chips/msp430/pins,
tos/chips/atm128/pins, and tos/chips/pxa27x/gpio.</p>
<p>Example implementations of the SPI interfaces can be found in tos/chips/msp430/usart,
tos/chips/atm128/spi, and tos/chips/pxa27x/ssp.</p>
<p>Example implementations of the I2C interfaces can be found in tos/chips/msp430/usart,
tos/chips/atm128/i2c, and tos/chips/pxa27x/i2c.</p>
<p>Example implementations of the UART interfaces can be found in tos/chips/msp430/usart,
tos/chips/atm128/uart/ and tos/chips/pxa27x/uart.</p>
</div>
<div class="section">
<h1><a id="author-s-address" name="author-s-address">5. Author's Address</a></h1>
<div class="line-block">
<div class="line">Phil Buonadonna</div>
<div class="line">Arch Rock Corporation</div>
<div class="line">657 Mission St. Ste 600</div>
<div class="line">San Francisco, CA 94105-4120</div>
<div class="line"><br /></div>
<div class="line">phone - +1 415 692-0828 x2833</div>
<div class="line"><br /></div>
<div class="line"><br /></div>
<div class="line">Jonathan Hui</div>
<div class="line">Arch Rock Corporation</div>
<div class="line">657 Mission St. Ste 600</div>
<div class="line">San Francisco, CA 94105-4120</div>
<div class="line"><br /></div>
<div class="line">phone - +1 415 692-0828 x2835</div>
</div>
</div>
<div class="section">
<h1><a id="citations" name="citations">6. Citations</a></h1>
<table class="docutils citation" frame="void" id="tep113" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a name="tep113">[tep113]</a></td><td>TEP 113: Serial Communication.</td></tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
